/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.comfacauca.xmlc2.servlet;

import com.comfacauca.xmlc2.utils.tablas.Database;
import com.comfacauca.xmlc2.utils.tablas.Editor;
import com.comfacauca.xmlc2.utils.tablas.Field;
import com.comfacauca.xmlc2.utils.tablas.Join;
import com.comfacauca.xmlc2.utils.tablas.Parameters;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author sarita
 */
public class ListaRequerimientosServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String dbName = "sarad";

           
            String[] tableNames = new String[]{"requerimientos_vw"};

           
            String pKey = "id";

            String[] fields = new String[]{"id", "codigo","titulo","descripcion_responsable", "inicio_requerimiento", "fin_requerimiento", "id_tipo"};

            Parameters params = new Parameters();

                    try {
                        String temp = request.getParameter("action");
                        if (temp != null) {
                            params.setAction(temp);
                        }
                        temp = request.getParameter("id");
                        if (temp != null) {
                            params.setID(temp);
                        }
                        temp = request.getParameter("start");
                        if (temp != null) {
                            params.setStart(Integer.parseInt(temp));
                        }
                        temp = request.getParameter("length");
                        if (temp != null) {
                            params.setLength(Integer.parseInt(temp));
                        }
                        temp = request.getParameter("draw");
                        if (temp != null) {
                            params.setDraw(Integer.parseInt(temp));
                        }
                        temp = request.getParameter("search[value]");
                        if (temp != null) {
                            params.setSearchValue(temp);
                        }
                        temp = request.getParameter("search[regex]");
                        if (temp != null) {
                            params.setSearchRegex(new Boolean(temp));
                        }

                        //Isolate all the parameters that begin with "columns"
                        HashMap<String, String> columns = new HashMap<String, String>();
                        List<String> requestParameterNames = Collections.list((Enumeration<String>) request.getParameterNames());
                        for (String parameterName : requestParameterNames) {
                            if (parameterName.startsWith("columns")) {
                                columns.put(parameterName, request.getParameter(parameterName));
                            }
                        }
                        if (columns.size() > 0) {
                            params.createColumnObjects(columns);
                        }

                        //Isolate all the parameters that begin with "order"
                        HashMap<String, String> columnorders = new HashMap<String, String>();
                        for (String parameterName : requestParameterNames) {
                            if (parameterName.startsWith("order")) {
                                columnorders.put(parameterName, request.getParameter(parameterName));
                            }
                        }
                        if (columnorders.size() > 0) {
                            params.createColumnOrderObjects(columnorders);
                        }

                        //Isolate all the parameters that begin with "data"
                        for (String parameterName : requestParameterNames) {
                            if (parameterName.startsWith("data")) {
                                params.addData(parameterName, request.getParameter(parameterName));
                            }
                        }

                        String[] values = request.getParameterValues("id[]");
                        if (values != null) {
                            for (int i = 0; i < values.length; i++) {
                                params.addId("id[" + i + "]", values[i]);
                            }
                        }
                    } catch (NumberFormatException nfe) {
                        nfe.printStackTrace();
                    } catch (Exception e) {
                        System.out.println("e in multi_filter.jsp");
                        e.printStackTrace();
                    }

                    /**
                     * ******************************************************************************
                     */
                    try {

                        //Create an instance of the database
                        Database db = new Database(dbName);

                        //Create an instance of Editor
                        Editor dataTable = new Editor(db, tableNames, pKey, params);

//                        dataTable.IncludeRowId(true);

                        //The rest are mandatory settings
                        Field field0 = new Field(tableNames[0], fields[0], Field.Type.STRING);
                        dataTable.addField(field0);
                        
                        Field field1 = new Field(tableNames[0], fields[1], Field.Type.STRING);
                        dataTable.addField(field1);
                        
                        Field field2 = new Field(tableNames[0], fields[2], Field.Type.STRING);
                        dataTable.addField(field2);
                        
                        Field field3 = new Field(tableNames[0], fields[3], Field.Type.STRING);
                        dataTable.addField(field3);
                        
                        Field field4 = new Field(tableNames[0], fields[4], Field.Type.STRING);
                        dataTable.addField(field4);
                        
                        Field field5 = new Field(tableNames[0], fields[5], Field.Type.STRING);
                        dataTable.addField(field5);
                        
                        Field field6 = new Field(tableNames[0], fields[6], Field.Type.STRING);
                        dataTable.addField(field6);
                        
                        dataTable.Process();

                        out.println(dataTable.toJSONString());

                    } catch (NullPointerException npe) {
                        System.out.println("npe in multi_filter.jsp");
                        npe.printStackTrace();
                    }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
